﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;
using System.Web.UI.HtmlControls;

namespace WebSys.UI.Control.ZhengShou
{
    public class ZS_SelectUserControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            if (!base.Page.IsPostBack)
            {
                BindData();
            }
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
            Button bt_query = (Button)FindControl("bt_query");
            bt_query.Click += new EventHandler(bt_query_Click);
        }
        protected void bt_query_Click(object sender, EventArgs e)
        {
            BindData();
        }
        protected void BindData()
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            CustomGridList1.RowCreated += new GridViewRowEventHandler(CustomGridList1_RowCreated);
            DataTable dt = _EluZSDataProvider.GetZSPerson(HttpContext.Current.Request["TYPE"], GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.HideColName = "ID";
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            BindData();
        }
        protected void CustomGridList1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            TableCell cell = new TableCell();
            string strType = HttpContext.Current.Request["TYPE"];

            if (e.Row.RowType == DataControlRowType.Header)
            {
                TableHeaderCell cell1 = new TableHeaderCell();
                cell1.Text = "选择";
                e.Row.Cells.AddAt(0, cell1);
                if (strType == "xfzdry")
                {
                    e.Row.Cells[12].Attributes.Add("style", "display:none");
                }
            }

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRowView rows = e.Row.DataItem as DataRowView;
                HtmlInputRadioButton rb = new HtmlInputRadioButton();

                if (rows != null)
                {
                    e.Row.Attributes.Add("id", rows["ID"].ToString());

                    rb.Attributes.Add("onclick", "setValue(this);");
                    rb.Attributes.Add("id", rows["ID"].ToString());
                }
                else
                {
                    e.Row.Attributes.Add("id", "");
                    rb.ID = "";
                    rb.Attributes.Add("onclick", "setValue(this);");
                }
                cell.Controls.Add(rb);
                e.Row.Cells.AddAt(0, cell);
                if (strType == "xfzdry")
                {
                    e.Row.Cells[12].Attributes.Add("style", "display:none");
                }
            }
        }
        protected string GetSqlWhere()
        {
            string sqlWhere = string.Empty;
            string strType = HttpContext.Current.Request["TYPE"];
            TextBox txt_name = (TextBox)FindControl("txt_name");
            TextBox txtCZRName = (TextBox)FindControl("txtCZRName");
            TextBox txtFWZL = (TextBox)FindControl("txtFWZL");
            if (!string.IsNullOrEmpty(txt_name.Text.Trim()))
            {
                sqlWhere += " and 产权人 like '%" + txt_name.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtCZRName.Text.Trim()))
            {
                sqlWhere += " and 承租人 like '%" + txtCZRName.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(txtFWZL.Text.Trim()))
            {
                sqlWhere += " and 房屋坐落 like '%" + txtFWZL.Text.Trim() + "%'";
            }
            if (strType == "zzfh")//住宅分户评估
            {
                sqlWhere += " and 房屋用途='1' ";
                sqlWhere += " and pid=(Select pId From zs_prono Where Id='" + base.Page.Request["pid"] + "')";
                sqlWhere += " and 信息完整 = '1'";
            }
            if (strType == "yyfh")//营业性住宅分户评估
            {
                sqlWhere += " and 房屋用途<>'1' ";
                sqlWhere += " and pid=(Select pId From zs_prono Where Id='" + base.Page.Request["pid"] + "')";
                sqlWhere += " and 信息完整 = '1'";
            }
            if (strType == "fyyfh")//非营业性住宅分户评估
            {
                sqlWhere += " and 房屋用途<>'1' ";
                sqlWhere += " and pid=(Select pId From zs_prono Where Id='" + base.Page.Request["pid"] + "')";
                sqlWhere += " and 信息完整 = '1'";
            }
            if (strType == "snfh")//装饰装修分户评估
            {
                sqlWhere += " and pid=(Select pId From zs_prono Where Id='" + base.Page.Request["pid"] + "')";
            }
            if (strType == "bdry")//产权调换合同备案时绑定人员
            {
//                sqlWhere += @" and id  in 
// (select ID from zs_zsperson where id in 
//(select zspid from zs_proloupan where pid in (select pid from zs_prono) 
//and id in (select fid from zs_cqzhxy ))) ";
                sqlWhere += " And pid='" + base.Page.Request["pid"] + "'";
                sqlWhere += " and 信息完整 = '1'";
            }
            if (strType == "xfzdry")
            {
                sqlWhere += " and id not in (select XFRYID from zs_xinfangzd)";
                if (_EluZSDataProvider.IsQZSBM(userid) && !_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid) && !_EluZSDataProvider.IsSZSB(userid))
                {
                    sqlWhere += @" and 区属 in(select keyvalue from zs_params where name='区属'and (Select tt.ROLENAME  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
        Where  t.userid Like '%" + userid + "%') Like '%'||keyname||'%')";
                }
            }
            
            //sqlWhere += " and id in (select ID from zs_zsperson Where pid in (Select pid from zs_prono where id = '" + Page.Request["pid"] + "')) ";
            sqlWhere += " order by addtime desc";
            return sqlWhere;
        }
    }
}


